package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import org.as4game.common.data.Cache;
	import org.as4game.common.data.List2;
	import org.as4game.common.math.IntRect;
	import org.as4game.common.math.Math2;
	import org.as4game.common.utils.Draw;
	import org.as4game.common.utils.Fps;

	public class TestData extends Sprite
	{
		public function TestData()
		{
//			test_list2();
//			test_cache();
//			test_fps();
//			test_rect();
			test_math();
		}
		
		private function test_math():void{
			var i:int;
			trace( Math.pow(2, 0), Math.pow(2, 1), Math.pow(2, 2) );
			i=0; trace( i, Math2.up2Power( i ) );
			i=1; trace( i, Math2.up2Power( i ) );
			i=4; trace( i, Math2.up2Power( i ) );
			i=10; trace( i, Math2.up2Power( i ) );
			i=100; trace( i, Math2.up2Power( i ) );
			i=1000; trace( i, Math2.up2Power( i ) );
			i=10000; trace( i, Math2.up2Power( i ) );
		}
		private function test_rect():void{
			
			var sp1:Sprite = new Sprite;
			Draw.fillRect( sp1.graphics, 0, 0, 100, 100, 0xff0000);
			addChild( sp1 );
			
			var sp2:Sprite = new Sprite;
			Draw.fillRect( sp2.graphics, 0, 0, 100, 100, 0x00ff00);
			addChild( sp2 );
			
			var sp3:Sprite = new Sprite;
			addChild( sp3 );
			sp3.mouseEnabled = false;
			
			this.addEventListener( MouseEvent.MOUSE_DOWN, onMouseEvent);
			this.addEventListener( MouseEvent.MOUSE_UP, onMouseEvent);
			this.addEventListener( Event.ENTER_FRAME, onEnterFrame);
			
			var sp:Sprite;
			function onMouseEvent(e:Event):void{
				if( e.type == MouseEvent.MOUSE_DOWN )
				{ 
					sp = e.target as Sprite;
					if( sp && sp != e.currentTarget )
						sp.startDrag();
				}
				else if( sp) 
				{
					sp.stopDrag();
				}
			}
			
			function onEnterFrame(e:Event):void{
				var dst:IntRect = IntRect.fromWH( sp1.x, sp1.y, sp1.width, sp1.height );
				var src:IntRect = IntRect.fromWH( sp2.x, sp2.y, sp2.width, sp2.height );
				var clip:IntRect = IntRect.fromWH( 10, 10, 80, 80 );
				clip.offset( src.left, src.top );
//				clip = null;
				
				var rc:IntRect = IntRect.getBltRect( dst, 0, 0, src, clip);
				sp3.graphics.clear();
				if( rc ){
					Draw.fillRect( sp3.graphics, rc.left, rc.top, rc.width, rc.height, 0x0000ff);
				}
			}
		}
		
		private function test_fps():void{
			var fps:Fps = new Fps();
			var tf:TextField = new TextField;
			tf.autoSize = "left";
			addChild( tf );
			
			stage.addEventListener( Event.ENTER_FRAME, onEnterFrame);
			function onEnterFrame(e:Event):void{
				fps.update();
				var str:String = "fps:" + fps.fps;
				tf.htmlText = str;
			}
		}
		
		private function test_cache():void{
			
			var cache:Cache = new Cache("test cache", 5);
			cache.fn_dispose = function(key:Object, obj:Object):void{
				trace("cache dispose object:", key, obj);
			};
				
			var i:int;
			for(i=0; i<10; i++){
				trace("add object", i);
				cache.addObject( i, "obj" + i );
			}
			trace( cache );
			for(i=0; i<10; i++){
				trace( i, cache.getObject( i ) );
			}
			trace( cache.getObject( 7 ) );
			trace( cache );
			trace( cache.has( 100 ) );
			trace( cache.has( 5 ) );
			cache.clear();
			cache.fixSize();
			trace( cache );
		}
			
		
		private function test_list2():void{
			var list:List2 = new List2;
			
			trace("pushTail"); 
			list.pushTail( new ListNode(3) ); trace(list);
			list.pushTail( new ListNode(4) ); trace(list);
			list.pushTail( new ListNode(5) ); trace(list);
			trace("pushHead"); 
			list.pushHead( new ListNode(2) ); trace(list);
			list.pushHead( new ListNode(1) ); trace(list);
			trace("popHead");
			list.popHead(); trace(list);
			list.popHead(); trace(list);
			trace("moveToHead");
			list.moveToHead( list.peekTail() ); trace(list);
			trace("popTail");
			list.popTail(); trace(list);
			list.popTail(); trace(list);
			list.popTail(); trace(list);
			list.popTail(); trace(list);
			list.popTail(); trace(list);
		}
	}
}

class ListNode{
	
	public var __prev:Object;
	public var __next:Object;
//	public var __key:Object;
	public var data:Object;
	
	public function ListNode( d:Object){
		data = d;
	} 
	
	public function toString():String{
		return String(data);
	}
}
